package com.fili.gui.components.table.color;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 22, 2006
 * Time: 10:40:19 PM
 */
public class CheckColorBox extends JPanel implements ActionListener, MouseListener
{
    public static final String PROP_SELECTION = "CCB_SELECTION";
    public static final String PROP_COLOR = "CCB_COLOR";
    private JCheckBox checkBox;
    private JLabel lblColor;
    private JLabel lblText;

    public CheckColorBox(boolean selected, Color color, String text)
    {
        setLayout(null);
        checkBox = new JCheckBox("", selected);
        checkBox.setBounds(0, 0, 18, 18);
        add(checkBox);
        checkBox.addActionListener(this);

        lblColor = new JLabel();
        lblColor.setOpaque(true);
        lblColor.setBackground(color);
        lblColor.setBorder(new LineBorder(Color.black));
        lblColor.setBounds(20, 3, 13, 13);
        add(lblColor);
        lblColor.addMouseListener(this);
        lblColor.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        lblText = new JLabel(text);
        lblText.setBounds(36, 3, 100, 15);
        lblText.setOpaque(true);
        lblText.setBackground(Color.white);
        add(lblText);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (checkBox.equals(e.getSource()))
            firePropertyChange(PROP_SELECTION, !checkBox.isSelected(), checkBox.isSelected());
    }

    public void mouseClicked(MouseEvent e)
    {
        Color oldCol = lblColor.getBackground();
        Color col = JColorChooser.showDialog(lblColor, "Alegeti culoarea", oldCol);
        if (col != null)
        {
            lblColor.setBackground(col);
            firePropertyChange(PROP_COLOR, oldCol, col);
        }
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }
}
